package com.gjun.struts.resource;

import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.gjun.struts.bean.Gjunshops;

public class GjunShopResourceDAO extends HibernateDaoSupport implements
		GjunShopResourceIf {

	public int getCount() throws Exception {
		// TODO Auto-generated method stub
		return ((Long) this.getHibernateTemplate().execute(
				new HibernateCallback() {

					public Object doInHibernate(Session session)
							throws HibernateException, SQLException {
						Query q = session
								.createQuery("select count(*) from Gjunshops");
						return q.uniqueResult();
					}
				})).intValue();
	}

	public int getCount(final String condition, final String value)
			throws Exception {
		// TODO Auto-generated method stub
		System.out.println(condition+"       ddd    "+value);
		return ((Long) this.getHibernateTemplate().execute(
				new HibernateCallback() {

					public Object doInHibernate(Session session)
							throws HibernateException, SQLException {
						Query q = session
								.createQuery("select count(*) from Gjunshops where "
										+ condition + "=?");
						q.setString(0, value);
						return q.uniqueResult();
					}
				})).intValue();
	}

	public int getCount(String province, String city, String status,
			String regStart, String regEnd) throws Exception {
		// TODO Auto-generated method stub
		
		final String hql = "select count(*) "+handlerHQL(province, city, status, regStart, regEnd);
		
		return ((Long) this.getHibernateTemplate().execute(
				new HibernateCallback() {

					public Object doInHibernate(Session session)
							throws HibernateException, SQLException {
						Query q = session.createQuery(hql);
						
						return q.uniqueResult();
					}
				})).intValue();
	}

	public List<Gjunshops> loadSomeShops(final int start, final int max) throws Exception {
		return (List<Gjunshops>) this.getHibernateTemplate().execute(
				new HibernateCallback() {

					public Object doInHibernate(Session session)
							throws HibernateException, SQLException {
						Query q = session.createQuery("from Gjunshops");
						q.setFirstResult(start);
						q.setMaxResults(max);
						
						return q.list();
					}
				});
	}

	public List<Gjunshops> loadSomeShops(final String condition, final String value,
			final int start, final int max) throws Exception {
		// TODO Auto-generated method stub
		return (List<Gjunshops>) this.getHibernateTemplate().execute(
				new HibernateCallback() {

					public Object doInHibernate(Session session)
							throws HibernateException, SQLException {
						Query q = session.createQuery("from Gjunshops where "+condition+"=?");
						q.setString(0,value);
						q.setFirstResult(start);
						q.setMaxResults(max);
						
						return q.list();
					}
				});
	}

	public List<Gjunshops> loadSomeShops(final String province, final String city,
			final String status, final String regStart, final String regEnd, final int start,final int max)
			throws Exception {
		return (List<Gjunshops>) this.getHibernateTemplate().execute(
				new HibernateCallback() {

					public Object doInHibernate(Session session)
							throws HibernateException, SQLException {
						Query q = session.createQuery(handlerHQL(province, city, status, regStart, regEnd));
						
						q.setFirstResult(start);
						q.setMaxResults(max);
						
						return q.list();
					}
				});
	}

	public boolean saveStore(Gjunshops shop) throws Exception {
		// TODO Auto-generated method stub
		return false;
	}

	public String handlerHQL(String province, String city, String status,
			String regStart, String regEnd) {
		String hql = "from Gjunshops";
		int length = hql.length();
		if (province != null && !"".equals(province)
				&& !"--请选择--".equals(province)) {
			hql += " province='" + province + "'";
		}
		if (city != null && !"".equals(city)) {
			hql += " and city='" + city + "'";
		}
		if (status != null && !"".equals(status) && !"all".equals(status)) {
			hql += " and status='" + status + "'";
		}

		if (regStart != null && regEnd != null && !regStart.equals("")
				&& !regEnd.equals("") && checkDate(regStart, regEnd)) {
			hql += " and regdate between '" + regStart + "' and '" + regEnd
					+ "'";
		}
		if (hql.length() > length) {
			hql = hql.substring(0, length) + " where "
					+ hql.substring(length + 1, hql.length());
		}
		hql = hql.replace("where and", "where");
		return hql;
	}

	public boolean checkDate(String regStart, String regEnd) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		try {
			Date start = sdf.parse(regStart);
			Date end = sdf.parse(regEnd);
			if (end.getTime() > start.getTime()) {
				return true;
			} else {
				return false;
			}
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
	}

}
